<template>
  <div class="message-container">
    <!-- 发表留言 -->
    <el-card class="message-form">
      <el-form :model="messageForm" :rules="rules" ref="messageFormRef">
        <el-form-item prop="content">
          <el-input
            v-model="messageForm.content"
            type="textarea"
            :rows="3"
            placeholder="请输入留言内容..."
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSubmit">发表留言</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 留言列表 -->
    <el-card v-for="message in messages" :key="message.id" class="message-item">
      <template #header>
        <div class="message-header">
          <span>{{ message.username }}</span>
          <span class="time">{{ message.createTime }}</span>
        </div>
      </template>
      <div class="message-content">
        {{ message.content }}
      </div>
      <div class="message-footer" v-if="isAdmin">
        <el-button link type="danger" @click="handleDelete(message)">删除</el-button>
      </div>
    </el-card>

    <el-pagination
      class="pagination"
      v-model:current-page="currentPage"
      v-model:page-size="pageSize"
      :total="total"
      :page-sizes="[10, 20, 30, 50]"
      layout="total, sizes, prev, pager, next, jumper"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import request from '../../utils/request'

const messages = ref([])
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(0)
const messageFormRef = ref()
const messageForm = ref({
  content: ''
})
const isAdmin = ref(true) // TODO: 从用户信息中获取

const rules = {
  content: [{ required: true, message: '请输入留言内容', trigger: 'blur' }]
}

const loadData = async () => {
  try {
    const res = await request.get('/api/message/list', {
      params: {
        page: currentPage.value,
        size: pageSize.value
      }
    })
    if (res.data.code === 200) {
      messages.value = res.data.data.records
      total.value = res.data.data.total
    }
  } catch (error) {
    console.error('加载数据失败:', error)
  }
}

const handleSubmit = () => {
  messageFormRef.value.validate(async (valid) => {
    if (valid) {
      try {
        const res = await request.post('/api/message', messageForm.value)
        if (res.data.code === 200) {
          ElMessage.success('发表成功')
          messageForm.value.content = ''
          loadData()
        }
      } catch (error) {
        ElMessage.error('发表失败')
      }
    }
  })
}

const handleDelete = (message) => {
  ElMessageBox.confirm('确定要删除该留言吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async () => {
    try {
      const res = await request.delete(`/api/message/${message.id}`)
      if (res.data.code === 200) {
        ElMessage.success('删除成功')
        loadData()
      }
    } catch (error) {
      ElMessage.error('删除失败')
    }
  })
}

const handleSizeChange = (val) => {
  pageSize.value = val
  loadData()
}

const handleCurrentChange = (val) => {
  currentPage.value = val
  loadData()
}

onMounted(() => {
  loadData()
})
</script>

<style scoped>
.message-container {
  padding: 20px;
}
.message-form {
  margin-bottom: 20px;
}
.message-item {
  margin-bottom: 20px;
}
.message-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.time {
  font-size: 14px;
  color: #999;
}
.message-content {
  padding: 10px 0;
}
.message-footer {
  display: flex;
  justify-content: flex-end;
}
.pagination {
  display: flex;
  justify-content: flex-end;
}
</style> 